var util = require('../../../utils/util.js');
var address = require('../../../utils/city.js');
var api = require('../../../config/api.js');
var animation
var app = getApp();

Page({
  data: {
    ifEid: false,
    isVisible: false,
    animationData: {},
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    addressId:'',
    areaInfo:'',
    areaInfoCode:'',
    id:'',
    /*地址的属性*/ 
    checked:0,
    phone:'',
    receivername:'',
    code:'',
    userId:'',
    
    addressParam: {
      id:'',
      areaInfo:'',
      areaInfoCode: '',
      province: '',
      city: '',
      area: '',
      street:'',
      checked: 0,
      phone: '',
      receivername: '',
      code:'',
      userId:''
    },


  },
 
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    if (options.id>0) {         //修改的时候，直接取数据出来
      this.setData({
        addressId: options.id,
        ifEid:false
      });
      this.getAddressDetail();
    }else{                    //新增的时候
      // 默认联动显示北京
      var id = address.provinces[0].id
      this.setData({
        provinces: address.provinces,
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    }
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    console.log(this.data)

  },
  onReady: function () {

  },

  bindinputName(event){
    let addressParam = this.data.addressParam;
    addressParam.receivername = event.detail.value;
    this.setData({
      addressParam: addressParam
    });
  },

  bindinputPhone(event) {
    let addressParam = this.data.addressParam;
    addressParam.phone = event.detail.value;
    this.setData({
      addressParam: addressParam
    });
  },

  bindinputAddress(event) {
    let addressParam = this.data.addressParam;
    addressParam.street = event.detail.value;
    this.setData({
      addressParam: addressParam
    });
  },

  bindinputCode(event) {
    let addressParam = this.data.addressParam;
    addressParam.code = event.detail.value;
    this.setData({
      addressParam: addressParam
    });
  },

  bindIsChecked(e) {
    let addressParam = this.data.addressParam;
    addressParam.checked = !addressParam.checked;
    this.setData({
      addressParam: addressParam
    });
  },

  //地址详情
  getAddressDetail() {
    let that = this;
    util.request(api.AddressDetail, { id: that.data.addressId }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          addressParam:res.data

        });
      }
    });
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    console.log('111111111')
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  chooseCancel: function (e) {
    this.startAddressAnimation(false)
  },
  
  cancelAddress() {
    wx.navigateTo({
      url: '/pages/ucenter/address/address',
    })
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到::输入框
    var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
    //地址的每一个code
    var areaInfoCode = that.data.provinces[value[0]].id + ',' + that.data.citys[value[1]].id + ',' + that.data.areas[value[2]].id
    that.setData({
      'addressParam.areaInfo': areaInfo,
      'addressParam.areaInfoCode':areaInfoCode,
    })
  },

  hideCitySelected: function (e) {
    console.log(e)
    this.startAddressAnimation(false)
  },

  // 处理省市县联动逻辑
  cityChange: function (e) {
    console.log("cityChange:"+e)
    var value = e.detail.value
    console.log("value"+value);
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
    console.log(this.data)
  },

  /* 新增/修改 */ 
  saveAddress() {
    console.log(this.data)
    let adata = this.data;

    if (adata.addressParam.receivername == '') {
      util.showErrorToast('请输入姓名');
      return false;
    }else{
      adata.addressParam.receivername = adata.addressParam.receivername
    }

    if (adata.addressParam.phone == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }else{
      adata.addressParam.phone = adata.addressParam.phone
    }

    if (adata.addressParam.areaInfo == '') {
      util.showErrorToast('请输入省市区');
      return false;
    }else{
      adata.addressParam.areaInfo = adata.addressParam.areaInfo
      adata.addressParam.areaInfoCode = adata.addressParam.areaInfoCode
    }

    if (adata.addressParam.street == '') {
      util.showErrorToast('请输入详细地址');
      return false;
    }else {
      adata.addressParam.street = adata.addressParam.street
    }

    if (adata.addressParam.code == '') {
      util.showErrorToast('请输入邮政编码');
      return false;
    }else {
      adata.addressParam.code = adata.addressParam.code
    }
      adata.addressParam.id = adata.addressId;
    let that = this;
    util.request(api.AddressSave, 
      adata.addressParam
    , 'POST').then(function (res) {
      if (res.errno === 0) {
        wx.navigateTo({
          url: '/pages/ucenter/address/address',
        })
      }
    });

  },


  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})